package com.py.credit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.py.credit.common.Page;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.constant.ApiPath;
import com.py.credit.dao.PluginApiDao;
import com.py.credit.entity.Feedback;
import com.py.credit.service.PluginApiService;
import com.py.framework.core.helper.UrlHelper;
import com.py.framework.core.http.HttpAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.*;

@Service
public class PluginApiServiceImpl implements PluginApiService {

    @Autowired
    private PluginApiDao pluginApiDao;

    @Autowired
    private UrlHelper urlHelper;

    @Override
    public RestServiceResult<Long> checkLogin(String username, String password) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("username", username);
        params.put("password", password);
        String url = urlHelper.getBaseServerUrl() + ApiPath.CHECK_LOGIN;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<Long>>() {
            });
        } catch (Exception e) {
            RestServiceResult<Long> data = new RestServiceResult<Long>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-远程调用检查登录用户是否存在并返回主键id调用异常！(PluginApiService#checkLogin) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public RestServiceResult<LinkedHashMap> getUserInfo(String userId) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("userId", userId);
        String url = urlHelper.getBaseServerUrl() + ApiPath.GET_USER_INFO_URL;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<LinkedHashMap>>() {
            });
        } catch (Exception e) {
            RestServiceResult<LinkedHashMap> data = new RestServiceResult<LinkedHashMap>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-远程调用获取用户信息调用异常！(PluginApiService#getUserInfo) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public List<LinkedHashMap> getDeptAjbhList(String deptId, String ztmc) {
        return pluginApiDao.getDeptAjbhList(deptId, ztmc);
    }

    @Override
    public RestServiceResult<LinkedHashMap> getCompanyInfo(String ztmc) {
        Map<String, String> params = new HashMap<String, String>(1);
        params.put("ztmc", ztmc);
        String url = urlHelper.getPlatformServerUrl() + ApiPath.GET_COMPANY_INFO_URL;
        String result = "";
        try {
            // 发送请求
            result = HttpAgent.getInstance().sendHttpPost(url, params, null);
            return JSONObject.parseObject(result, new TypeReference<RestServiceResult<LinkedHashMap>>() {
            });
        } catch (Exception e) {
            RestServiceResult<LinkedHashMap> data = new RestServiceResult<LinkedHashMap>();
            data.setCode(RestServiceResult.CODE_UNKNOW);
            data.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("远程服务-远程调用获取企业信息调用异常！(PluginApiService#getCompanyInfo) URL:" + url
                    + " Params:" + params.toString() + "Result:" + result, e.getMessage(), e);
            return data;
        }
    }

    @Override
    public String getLabel(String deptId,String ztmc,String jclb) {
        return pluginApiDao.getLabel(deptId,ztmc,jclb);
    }

    @Override
    public List<LinkedHashMap> getCsList(String deptId, String label,String jclb) {
        return pluginApiDao.getCsList(deptId, Arrays.asList(label.split(",")),jclb);
    }

    @Override
    public String selectAjbhByCsIdAndZtmc(String deptId,String csId, String ztmc,String jclb) {
        return pluginApiDao.selectAjbhByCsIdAndZtmc(deptId,csId, ztmc,jclb);
    }

    @Override
    public int insertFeedbackInfo(long userId, long deptId, String ajbh, Timestamp feedbackTime, String mess,String batchId,String managerName) {
        Feedback feedback=new Feedback();
        feedback.setManagerId(userId);
        feedback.setDeptId(deptId);
        feedback.setAjbh(ajbh);
        feedback.setFeedbackTime(feedbackTime);
        feedback.setFeedbackMess(mess);
        feedback.setBatchId(batchId);
        feedback.setManagerName(managerName);
        pluginApiDao.insertFeedbackInfo(feedback);
        return feedback.getId();
    }

    @Override
    public void insertFeedbackCs(int feedbackId,String csId,String batchId) {
        pluginApiDao.insertFeedbackCs(feedbackId, csId,batchId);
    }

    @Override
    public void modifyMess(Timestamp feedbackTime,String ajbh) {
        pluginApiDao.modifyMess("Y", feedbackTime, ajbh);
    }

    @Override
    public List<String> selectAllUrl(long userId) {
        return pluginApiDao.selectAllUrl(userId);
    }

    @Override
    public List<LinkedHashMap> selectUrlForPage(long userId,Page page) {
        return pluginApiDao.selectUrlForPage(userId,page);
    }

    @Override
    public void insertUrl(String urlName, String url,long useId) {
        pluginApiDao.insertUrl(urlName,url,useId);
    }

    @Override
    public void modifyUrl(String urlName, String url, String status, String delFlag, String id) {
        pluginApiDao.modifyUrl(urlName,url,status,delFlag,id);
    }

    @Override
    public List<LinkedHashMap> selectJcListForPage(String deptId,String ztmc, String jclb, Page page,String startTime, String endTime) {
        return pluginApiDao.selectJcListForPage(deptId, ztmc, jclb, page,startTime,endTime);
    }

    @Override
    public List<LinkedHashMap> selectJcDetail(String ztmc,String jclb,String label) {
        return pluginApiDao.selectJcDetail(ztmc, jclb,Arrays.asList(label.split(",")));
    }

    @Override
    public List<LinkedHashMap> selectFeedbackListForPage(String deptId,String ztmc,Page page,String jclb,String status,String startTime, String endTime) {
//        List<LinkedHashMap> feedbackList = pluginApiDao.selectFeedbackListForPage(deptId, ztmc, page);
//        for (LinkedHashMap map:feedbackList
//             ) {
//            String label = pluginApiDao.getLabel(deptId, ztmc, (String) map.get("jclb"));
//            Timestamp feedbackTime = pluginApiDao.selectFeedbackTime((String) map.get("ztmc"), (String) map.get("jclb"),Arrays.asList(label.split(",")));
//            if(null!=feedbackTime){
//                map.put("feedbackType","Y");
//                map.put("feedbackTime",feedbackTime);
//            }else{
//                map.put("feedbackType","N");
//            }
//        }
//        return feedbackList;

        return pluginApiDao.selectFeedbackListForPage(deptId, ztmc, page,jclb,status,startTime,endTime);
    }

    @Override
    public List<Feedback> selectFeedbackDetail(String ztmc, String jclb,String label) {
        List<Feedback> feedbackList = pluginApiDao.selectFeedbackDetail(ztmc, jclb, Arrays.asList(label.split(",")));
        for (Feedback feedback:feedbackList
                ) {
            feedback.setMeasureList(pluginApiDao.selectMeasureDetail(feedback.getBatchId()));
        }
        return feedbackList;
    }

    @Override
    public void insertApiLog(long userId, String ztmc,String bm, String ipaddress) {
        pluginApiDao.insertApiLog(userId, ztmc,bm, ipaddress);
    }

    /**
     * 查询用户勋章等级
     * @param userId
     * @return
     */
    @Override
    public int selectUserGrade(long userId) {
        int pluginApiCount = pluginApiDao.selectPluginApiCount(userId);
        int feedbackCount = pluginApiDao.selectFeedbackCount(userId);

        if(pluginApiCount>40000 && feedbackCount>40000){
            return 8;
        }else if(pluginApiCount>30000 && feedbackCount>30000){
            return 7;
        }else if(pluginApiCount>20000 && feedbackCount>20000){
            return 6;
        }else if(pluginApiCount>10000 && feedbackCount>10000){
            return 5;
        }else if(pluginApiCount>1000 && feedbackCount>1000){
            return 4;
        }else if(pluginApiCount>100 && feedbackCount>100){
            return 3;
        }else if(pluginApiCount>10 && feedbackCount>10){
            return 2;
        }else if(pluginApiCount>1 && feedbackCount>1){
            return 1;
        }else{
            return 1;
        }
    }
}
